/**
 * Failed to minify the file using UglifyJS v3.4.4. Serving the original version.
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
(function() {
    'use strict';
    // save the original methods before overwriting them
    window._addEventListener = window.addEventListener;
    window._removeEventListener = window.removeEventListener;
    window.hookeventListenerList={}
    /**
     * [addEventListener description]
     * @param {[type]}  type       [description]
     * @param {[type]}  listener   [description]
     * @param {Boolean} useCapture [description]
     */
    window.addEventListener = function(type,listener,useCapture=false) {
        // declare listener
        this._addEventListener(type,listener,useCapture);

        if(!window.hookeventListenerList) window.hookeventListenerList = {};
        if(!window.hookeventListenerList[type]) window.hookeventListenerList[type] = [];
        console.log( window.hookeventListenerList)
        // add listener to  event tracking list
        window.hookeventListenerList[type].push( {type, listener, useCapture} );
    };

    /**
     * [removeEventListener description]
     * @param  {[type]}  type       [description]
     * @param  {[type]}  listener   [description]
     * @param  {Boolean} useCapture [description]
     * @return {[type]}             [description]
     */
    window.removeEventListener = function(type,listener,useCapture=false) {
        // remove listener
        this._removeEventListener(type,listener,useCapture);

        if(!window.hookeventListenerList) window.hookeventListenerList = {};
        if(!window.hookeventListenerList[type]) window.hookeventListenerList[type] = [];

        // Find the event in the list, If a listener is registered twice, one
        // with capture and one without, remove each one separately. Removal of
        // a capturing listener does not affect a non-capturing version of the
        // same listener, and vice versa.
        for(let i=0; i<window.hookeventListenerList[type].length; i++){
            if( window.hookeventListenerList[type][i].listener===listener && window.hookeventListenerList[type][i].useCapture===useCapture){
                window.hookeventListenerList[type].splice(i, 1);
                break;
            }
        }
        // if no more events of the removed event type are left,remove the group
        if(window.hookeventListenerList[type].length==0) delete window.hookeventListenerList[type];
    };


    /**
     * [getEventListeners description]
     * @param  {[type]} type [description]
     * @return {[type]}      [description]
     */
    window.getEventListeners = function(type){
        if(!window.hookeventListenerList) window.hookeventListenerList = {};

        // return reqested listeners type or all them
        if(type===undefined)  return window.hookeventListenerList;
        return window.hookeventListenerList[type];
    };


    /*
    window.prototype.clearEventListeners = function(a){
        if(!window.hookeventListenerList)
            window.hookeventListenerList = {};
        if(a==undefined){
            for(var x in (this.getEventListeners())) this.clearEventListeners(x);
            return;
        }
        var el = this.getEventListeners(a);
        if(el==undefined)
            return;
        for(var i = el.length - 1; i >= 0; --i) {
            var ev = el[i];
            this.removeEventListener(a, ev.listener, ev.useCapture);
        }
    };
    */

})();